package com.software.demo;

import com.software.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Run {
    public static void main(String[] args) {
        Run run = new Run();
        run.save();
    }
    public void save() {
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        Student s1 = new Student();
        s1.setName("hugx");
        s1.setStudentNo("001");

        Student s2 = new Student();
        s2.setName("suye");
        s2.setStudentNo("002");

        Course c1 = new Course();
        c1.setName("企业级开发二");
        c1.setCredit(4.0);

        Course c2 = new Course();
        c2.setName("持久化框架开发");
        c2.setCredit(4.0);

        s1.getCourseSet().add(c1);
        s1.getCourseSet().add(c2);
        s2.getCourseSet().add(c1);
        s2.getCourseSet().add(c2);

        c1.getStudentSet().add(s1);
        c1.getStudentSet().add(s2);
        c2.getStudentSet().add(s1);
        c2.getStudentSet().add(s2);

        session.save(s1);
        session.save(s2);
        session.save(c1);
        session.save(c2);

        transaction.commit();
        session.close();
    }
}
